
/**
 * 2016年9月5
 */
package algorithm;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *4和7是两个幸运数字,我们定义,十进制表示中,每一位只有4和7两个数的正整数是幸运数组,前几个为
 *4,7,44,47,74,77,444,447... 
 * @author Alex
 *
 *等比数列前n项各Sn=a1(1-q^n)/(1-q) 
 *令Sn=K
 *n = 
 */
class TreeNodes{
	
}
public class LuckNumber {
	public static void main(String args[]){

		for(int i=1;i<=20;i++){
			System.out.print(trans(i)+",");
		}
	}
	public static String trans(int k){
		int group = (int)Math.ceil(n(k));
		int seq = k - (int)Sn(group-1);
		//System.out.println(String.format("Group:%d,Seq:%d", group,seq));
			int bits = group;
			int num = seq-1;
			char []res = D2b(bits,num).toCharArray();
			for(int i =0;i<res.length;i++){
				if(res[i]=='0')
					res[i]='4';
				if(res[i]=='1')
					res[i]='7';
			}
			String fi = new String(res);
			return fi;
	}
	public static String D2b(int bits,int num){
		String bstr = Integer.toBinaryString(num);
		while(bstr.length()<bits){
			bstr="0"+bstr;
		}
		return bstr;
	}
	
	public static double n(double d){
		double up = Math.log((2+d)/2.0);
		double down = Math.log(2.0);
		return up/down;		
	}
	public static double Sn(double n){
		return 2*(1-Math.pow(2, n))/(-1);
	}
}

